package com.wyq.action;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.wyq.service.MailInfoService;

public class EmailMangerAction extends ActionSupport {
	private MailInfoService mailInfoService;
	private List mailInfoList ;
	private String formStr;
	
	/**
	 * 获取邮箱信息
	 * @return
	 */
	public String getEmailServerInfo(){
		try {
			List mailList = mailInfoService.getMailInfo();
			mailInfoList = mailList;
			//JSONArray mailJson = JSONArray.fromObject(mailList);
			//mailInfo = "{data:'"+mailJson.toString()+"'}";
			//System.out.println("---------" + mailList);
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}
		
	}
	
	/**
	 * 保存信息
	 * @return
	 */
	public String saveEmainServerInfo(){
		try{
			ActionContext context = ActionContext.getContext();   
			HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
			PrintWriter out=response.getWriter();
			
			try {
				formStr = java.net.URLDecoder.decode(formStr,"UTF-8");
				Map mailInfoMap = (Map)JSONUtil.deserialize(formStr);
				System.out.println("************" + mailInfoMap.get("mailServerAddress"));
				int flag = mailInfoService.updateMailInfo(mailInfoMap);
				out.print("{success:true,data:'" + flag + "'}");
			} catch (Exception e) {
				e.printStackTrace(); 
				out.print("{success:false,reason:'保存失败'}");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public void setFormStr(String formStr) {
		this.formStr = formStr;
	}

	public List getMailInfoList() {
		return mailInfoList;
	}

	public void setMailInfoService(MailInfoService mailInfoService) {
		this.mailInfoService = mailInfoService;
	}
	

}
